package cn.iocoder.yudao.module.wine.service.supplyorderlog;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.supplyorderlog.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorderlog.SupplyOrderLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.supplyorderlog.SupplyOrderLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 供求订单日志 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SupplyOrderLogServiceImpl implements SupplyOrderLogService {

    @Resource
    private SupplyOrderLogMapper supplyOrderLogMapper;

    @Override
    public Long createSupplyOrderLog(SupplyOrderLogSaveReqVO createReqVO) {
        // 插入
        SupplyOrderLogDO supplyOrderLog = BeanUtils.toBean(createReqVO, SupplyOrderLogDO.class);
        supplyOrderLogMapper.insert(supplyOrderLog);
        // 返回
        return supplyOrderLog.getId();
    }

    @Override
    public void updateSupplyOrderLog(SupplyOrderLogSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplyOrderLogExists(updateReqVO.getId());
        // 更新
        SupplyOrderLogDO updateObj = BeanUtils.toBean(updateReqVO, SupplyOrderLogDO.class);
        supplyOrderLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplyOrderLog(Long id) {
        // 校验存在
        validateSupplyOrderLogExists(id);
        // 删除
        supplyOrderLogMapper.deleteById(id);
    }

    private void validateSupplyOrderLogExists(Long id) {
        if (supplyOrderLogMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public SupplyOrderLogDO getSupplyOrderLog(Long id) {
        return supplyOrderLogMapper.selectById(id);
    }

    @Override
    public PageResult<SupplyOrderLogRespVO> getSupplyOrderLogPage(SupplyOrderLogPageReqVO pageReqVO) {
        return supplyOrderLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SupplyOrderLogDO> getSupplyOrderLogList(Long orderId) {

        return supplyOrderLogMapper.selectList(new LambdaQueryWrapper<SupplyOrderLogDO>()
                .eq(SupplyOrderLogDO::getOrderId,orderId)
                .orderByDesc(SupplyOrderLogDO::getCreateTime));
    }

    @Override
    public void logRecord(Long userId, Long orderId, String userType, String content) {
        SupplyOrderLogDO supplyOrderLogDO = new SupplyOrderLogDO();
        supplyOrderLogDO.setUserType(userType);
        supplyOrderLogDO.setOrderId(orderId);
        supplyOrderLogDO.setUserId(userId);
        supplyOrderLogDO.setContent(content);
        supplyOrderLogMapper.insert(supplyOrderLogDO);
    }

}
